package au.org.arcs.dss.provider.datasource.provider.stub;

import java.util.*;

import au.org.arcs.dss.provider.dataset.DataSet;

/**
 *
 * @author ming
 */
public class StubService {
  private DataSet rootDataSet;
  private DataSet folder1;
  private DataSet folder2;
  private DataSet file1;
  private DataSet file2;
  private DataSet folder11;
  private DataSet file3;
  private List<DataSet> rootDescendants = new ArrayList<DataSet>();
  private List<DataSet> folder1Descendants = new ArrayList<DataSet>();

  public StubService() {
    rootDataSet = new DataSet();

    rootDataSet.setFile(false);
    rootDataSet.setDirectory(true);
    rootDataSet.setName("root");
    rootDataSet.setPath("/root");
    rootDataSet.setLastModified(1000);
    rootDataSet.setLength(100);

    folder1 = new DataSet();
    folder1.setFile(false);
    folder1.setDirectory(true);
    folder1.setName("folder1");
    folder1.setPath("/root/folder1");
    folder1.setLastModified(1000);
    folder1.setLength(100);

    folder2 = new DataSet();
    folder2.setFile(false);
    folder2.setDirectory(true);
    folder2.setName("folder2");
    folder2.setPath("/root/folder2");
    folder2.setLastModified(1000);
    folder2.setLength(100);

    file1 = new DataSet();
    file1.setFile(true);
    file1.setDirectory(false);
    file1.setName("file1");
    file1.setPath("/root/file1");
    file1.setLastModified(1000);
    file1.setLength(100);

    file2 = new DataSet();
    file2.setFile(true);
    file2.setDirectory(false);
    file2.setName("file2");
    file2.setPath("/root/file2");
    file2.setLastModified(1000);
    file2.setLength(100);

    file3 = new DataSet();
    file3.setFile(true);
    file3.setDirectory(false);
    file3.setName("file3");
    file3.setPath("/root/folder1/file3");
    file3.setLastModified(1000);
    file3.setLength(100);

    folder11 = new DataSet();
    folder11.setFile(false);
    folder11.setDirectory(true);
    folder11.setName("folder11");
    folder11.setPath("/root/folder1/folder11");
    folder11.setLastModified(1000);
    folder11.setLength(100);

    rootDescendants.add(folder1);
    rootDescendants.add(folder2);
    rootDescendants.add(file1);
    rootDescendants.add(file2);

    folder1Descendants.add(folder11);
    folder1Descendants.add(file3);
  }

  public List<DataSet> getDataSets(String path) {
    if (path.equals("/root")) {
      return rootDescendants;
    }

    if (path.equals("/root/folder1")) {
      return folder1Descendants;
    }

    return null;
  }

  public DataSet getDataSet(String path, boolean withDecendents, boolean withParent) {
    if (path.equals("/root")) {
      if (withDecendents) {
        rootDataSet.setDescendants(rootDescendants);
      }

      return rootDataSet;
    }

    if (path.equals("/root/folder1/file3")) {
      if (withParent) {
        file3.setParent(folder1);
      }

      return file3;
    }

    if (path.equals("/root/folder1/folder11")) {
      if (withParent) {
        folder11.setParent(folder1);
      }

      return folder11;
    }

    if (path.equals("/root/folder1")) {
      if (withDecendents) {
        folder1.setDescendants(folder1Descendants);
      }

      if (withParent) {
        folder1.setParent(rootDataSet);
      }

      return folder1;
    }

    if (path.equals("/root/folder2")) {

      if (withParent) {
        folder2.setParent(rootDataSet);
      }

      return folder2;
    }

    if (path.equals("/root/file1")) {
      if (withParent) {
        file1.setParent(rootDataSet);
      }

      return file1;
    }

    if (path.equals("/root/file2")) {

      if (withParent) {
        file2.setParent(rootDataSet);
      }

      return file2;
    }

    return null;
  }

  public static String getPath(String path, String name) {
    if (name != null && !name.isEmpty()) {
      return path + '/' + name;
    } else {
      return path;
    }
  }
}
